Allow esbuild to reuse mangled property names in certain cases#4454
Open
LoganDark wants to merge 2 commits intoevanw:mainfrom
Open
Allow esbuild to reuse mangled property names in certain cases#4454LoganDark wants to merge 2 commits intoevanw:mainfrom
LoganDark wants to merge 2 commits intoevanw:mainfrom
Conversation
Patterns like ^abc|abc$ could have caused abc123 and 123abc to be cached as a single name; treat them like separate namespaces for correctness.
70e7e91 to
6d13632
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds new options
manglePropNamespacesandmangleNamespaceCachesalong with corresponding tests. This allows esbuild to associate property names with a namespace during mangling, and reuse mangled names across namespaces, potentially resulting in bundle size savings. Namespaced caches are persisted tomangleNamespaceCacheswhen one is specified, and when--mangle-cacheis specified via CLI, namespace caches are stored with a leading#like so (with another leading^for prefix matches and a trailing$for suffix matches):{ "global_prop_": "a", "#^TypeA_": { "foo_": "b" }, "#^TypeB_": { "foo_": "b" } }manglePropNamespacesis given a regular expression that matches on the property name to be mangled. If this regular expression matches, the match is treated as the property's namespace and the mangled name will be generated in that namespace (only avoiding collisions with others in the same namespace and with global properties).Properties without a namespace are considered global, and will be given entirely unique mangled names (that do not conflict with any namespaces). Only namespaced properties will reuse mangled names from other namespaces - global properties will never reuse mangled names, and namespaced properties will never reuse mangled names of global properties.
The regular expression is expected to be anchored to either the start or the end of the property name. There is a basic check that such an anchor exists in the pattern, but if it manages to match in the middle of a property name anyway (e.g.
^start|sneaky), it is just treated as global. Also, RE2 doesn't support lookaheads, so if the namespace pattern matches the entire property name, then it is also treated as global.Note that improper use can result in runtime errors, namely in cases where properties of different namespaces are mixed on the same object, or where code expecting properties from one namespace encounters properties from another namespace. Users are expected to know their use-case well enough to determine which properties to namespace or not.
In general, there's not really any harm to namespace collisions (i.e. two fields being in the same namespace when you didn't intend for them to be) other than perhaps missing some opportunities for name reuse, but there is harm in fields incorrectly being in different namespaces.
This PR has been generated with assistance from Claude, but the code has been manually audited and aggressively distilled, and I have successfully tested it with real projects.
I did not find contributing guidelines in the repo so please let me know if I missed anything.
Closes #4453